<!--
 * @Desc: 
 * @Author: 小超越
 * @LastEditors: 小超越
 * @BlogUrl: www.zhe94.com
-->
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="__ADMIN__/pear/css/pear.css" />
</head>

<body class="pear-container">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>

    <script type="text/html" id="toolbar">
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="renew">
			<i class="layui-icon layui-icon-refresh"></i>
			恢复数据
		</button>
        <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
			<i class="layui-icon layui-icon-delete"></i>
			彻底删除
		</button>
    </script>
    <script src="__ADMIN__/layui/layui.js"></script>
    <script src="__ADMIN__/pear/pear.js"></script>
    <script>
        layui.use(['table', 'form', 'jquery', 'laydate'], function() {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let cols = [
                [{
                    type: 'checkbox'
                }, {
                    field: 'id',
                    minWidth: 80,
                    title: 'id'
                }, {
                    field: 'name',
                    minWidth: 200,
                    title: '分类名称'
                }, {
                    field: 'seo_title',
                    minWidth: 200,
                    title: 'seo标题'
                }, {
                    field: 'seo_alias',
                    minWidth: 200,
                    title: 'seo别名'
                }]
            ];
            table.render({
                elem: '#dataTable',
                url: 'recycle',
                page: true,
                cols: cols,
                skin: 'line',
                toolbar: '#toolbar',
                defaultToolbar: [{
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });

            table.on('toolbar(dataTable)', function(obj) {
                if (obj.event === 'batchRemove') {
                    window.batchRemove(obj);
                } else if (obj.event === 'renew') {
                    window.renew(obj);
                } else if (obj.event === 'refresh') {
                    window.refresh();
                }
            });


            if (typeof width !== 'number' || width === 0) {
                width = $(window).width() * 0.8;
            }
            if (typeof height !== 'number' || height === 0) {
                height = $(window).height() - 20;
            }

            window.renew = function(obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                var ids = []
                var hasCheck = table.checkStatus('dataTable')
                var hasCheckData = hasCheck.data
                if (hasCheckData.length > 0) {
                    $.each(hasCheckData, function(index, element) {
                        ids.push(element.id)
                    })
                }
                layer.confirm('确定要恢复该分类', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        data: {
                            ids: ids,
                            type: 1
                        },
                        dataType: 'json',
                        type: 'POST',
                        success: function(res) {
                            layer.close(loading);
                            //判断有没有权限
                            if (res && res.code == 999) {
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000,
                                })
                                return false;
                            } else if (res.code == 0) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    table.reload('dataTable');
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.batchRemove = function(obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                var ids = []
                var hasCheck = table.checkStatus('dataTable')
                var hasCheckData = hasCheck.data
                if (hasCheckData.length > 0) {
                    $.each(hasCheckData, function(index, element) {
                        ids.push(element.id)
                    })
                }
                layer.confirm('确定要删除该分类', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        data: {
                            ids: ids,
                            type: 2
                        },
                        dataType: 'json',
                        type: 'POST',
                        success: function(res) {
                            layer.close(loading);
                            //判断有没有权限
                            if (res && res.code == 999) {
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000,
                                })
                                return false;
                            } else if (res.code == 0) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    table.reload('dataTable');
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.refresh = function() {
                table.reload('dataTable');
            }
        })
    </script>
</body>

</html>